package com.pagoda.nerp.trade.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.pagoda.nerp.trade.common.exception.UpdateException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.pagoda.nerp.trade.common.ConstantDefine;
import com.pagoda.nerp.trade.dao.PurVendorListpriceDao;
import com.pagoda.nerp.trade.dao.SysBillRuleDao;
import com.pagoda.nerp.trade.entity.PurVendorListprice;
import com.pagoda.nerp.trade.service.PurVendorListpriceService;

@Service
public class PurVendorListpriceServiceImpl implements PurVendorListpriceService{
	
	@Autowired
	private PurVendorListpriceDao purVendorListpriceDao;
	
	@Autowired
	private SysBillRuleDao sysBillRuleDao;

	@Override
	public int insert(PurVendorListprice listPrice) {
		int listPriceId = sysBillRuleDao.generateId(ConstantDefine.BILLCODE_VENDOR_LISTPRICE_ID);
		String listpriceOrderNo = sysBillRuleDao.generateCode(ConstantDefine.BILLCODE_LISTPRICE_ORDER_NO);
		listPrice.setVendorListPriceId(listPriceId);
		listPrice.setListpriceOrderNo(listpriceOrderNo);
		listPrice.setListpriceStatus(ConstantDefine.LIST_PRICE_INI);
		listPrice.setStatus(ConstantDefine.LIST_RICE_STATUS_INI);
		listPrice.setOrderDate(new Date());
		return purVendorListpriceDao.insert(listPrice);
	}

	@Override
	public List<PurVendorListprice> selectListByContifion(int vendorId) {
		return purVendorListpriceDao.selectListByContifion(vendorId);
	}

	@Override
	public PurVendorListprice selectOneByContifion(int vendorListPriceId) {
		return purVendorListpriceDao.selectOneByContifion(vendorListPriceId);
	}

	@Override
	public int updateStatus(Map map) {
		int resultCode = purVendorListpriceDao.updateStatus(map);
		if (resultCode==0) {
			throw new UpdateException("修改报价单状态失败");
		}
		return resultCode;
	}

	@Override
	public List<Map<String, Object>> selectListByRequest(String goodsRequestCode) {
		List<PurVendorListprice> list = purVendorListpriceDao.selectListByRequest(goodsRequestCode);
		List<Map<String, Object>> data=new ArrayList<Map<String, Object>>();
		SimpleDateFormat df=new SimpleDateFormat(ConstantDefine.DATE_FORMAT);
		if (list!=null) {
			for (PurVendorListprice price: list) {
				Map<String, Object> map = new HashMap<String,Object>();
				map.put("listPriceOrderNo", price.getListpriceOrderNo());
				map.put("pltRequestCode", price.getGoodsRequestCode());
				map.put("quotationDate", df.format(price.getListpriceTime()));
				map.put("vendorCode", price.getVendorCode());
				map.put("goodsCode", price.getGoodsCode());
				map.put("goodsName", price.getGoodsName());
				map.put("purUnitName", price.getPurUnitName());
				map.put("requestQty", price.getRequestQty());
				map.put("orderQty", price.getOrderQty());
				map.put("listPrice", price.getBuyPrice());
				map.put("contactPhone", price.getPhoneNumber());
				data.add(map);
			}
		}
		
		return data;
	}

	
}
